import { ipcRenderer, contextBridge, IpcRendererEvent } from 'electron'
import { WinEnum, ElectronAPI, DialogEnum, CustomEnum, CounterEnum } from '/@/enums/channelEnum'

export interface ElectronAPI {
  setTitle: (title: string) => void
  openFile: () => Promise<string[]>
  openFile2: (info: string, callback: (...args: any) => void) => void
  updateCounter: (callback: (...args: any) => void) => void
}

// 向渲染进程暴露一个全局的 window.electronAPI
contextBridge.exposeInMainWorld(ElectronAPI, {
  setTitle: (title: string) => {
    ipcRenderer.send(WinEnum.SETT_ITLE, title)
  },
  openFile: () => ipcRenderer.invoke(DialogEnum.OPEN_FILE),
  openFile2: (info: string, callback: (...args: any) => void) => {
    ipcRenderer.send(CustomEnum.OPEN, info)
    ipcRenderer.on(CustomEnum.REPLY, (event: IpcRendererEvent, args: string[]) => {
      // console.log('event:', event)
      // console.log('args:', args)
      callback(event, args)
    })
  },
  updateCounter: (callback: (...args: any) => void) => {
    ipcRenderer.on(CounterEnum.UPDATE, (event, args) => {
      callback(event, args)
    })
  }
})
